<?php
/**
 * 文章评论
 * Created by PhpStorm.
 * User: andy
 * Date: 2019/11/21
 * Time: 下午6:42
 */
namespace app\common\model;

use think\Model;
use think\db;

class UserCarMessageModel extends Model
{
    const SHOW_STATUS=1; //正常
    const HIDE_STATUS=0; //禁用
    const DEL_STATUS=-1; //删除

    protected $autoWriteTimestamp = true;

    public function user()
    {
        return $this->hasOne('MemberModel','uid','user_id')->field('uid,mobile');
    }

    public function car()
    {
        return $this->hasOne('UserCarModel','id','user_car_id')->field('id,vin,user_id,brand_id,series_id,car_id');
    }

    /**
     * 所有评论
     * @param array $where
     * @param int $pageSum
     * @return \think\Paginator
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getALL($where=[],$pageSum=0)
    {
        if($pageSum<=0){
            $pageSum=getSystem("pageSUM")?getSystem("pageSUM"):10;
        }

        $list=self::with('user')
            ->with('car')
            ->where('status','>',self::DEL_STATUS)
            ->where($where)
            ->order('status asc,create_time desc')
            ->paginate($pageSum);

        foreach ($list as $vo){
            $brand=CarBrandModel::field("brand")->where('brand_id',$vo['car']['brand_id'])->find();
            $series=CarSeriesModel::field('series_name')->where('series_id',$vo['car']['series_id'])->find();
            $vo->car_name="";


            if(!empty($vo['car']['vin'])){
                $carVin=CarLicenseMoreModel::field("sale_name,guiding_price")->where('vin','=',$vo['car']['vin'])->find();
                $vo->car_name = $carVin->sale_name;
                $vo->series_max = $carVin->guiding_price."万";
            }else{
                if($vo['car']['car_id']){
                    $car = CarModel::field('car_name,series_max,car_case')->where('car_id',$vo['car']['car_id'])->find();
                    if (!empty($car)){
                        $vo->car_name = $car->car_name;
                        $vo->series_max = $car->series_max;
                    }

                }
            }

            $vo->pcontent='';
            if($vo['pid']>0){
                $pcontent=self::field('id,content')->find($vo['pid']);
                $vo->pcontent=$pcontent->content;
            }

            $u=MemberModel::field('mobile')->where('uid',$vo['car']['user_id'])->find();

            $vo->brand_name=$brand->brand;
            $vo->series_name=$series->series_name;
            $vo->fmobile=$u->mobile;
        }

        return $list;
    }


    /**
     * 删除
     * @param $id
     * @return array
     */
    public function del($id){

        $au=self::get($id);
        $au->status=self::DEL_STATUS;

        $status=$au->save();
        if($status){
            return ['status'=>1,'msg'=>'删除成功！'];
        }else{
            return ['status'=>0,'msg'=>'删除失败！'];
        }
    }

    /**
     * 留言
     * @param $data
     * @return array
     */
    public function adds($data){
        $status = self::insertGetId($data);
        $user_car_id = $data["user_car_id"];
        //更新发布车辆最新回复时间
        $userCarModel = model("userCar");
        $userCarModel->upLastTime($user_car_id);
        if($status){
            return['status'=>1,'msg'=>'增加成功！','id'=>$status];
        }else{
            return['status'=>0,'msg'=>'增加失败！','id'=>0];
        }
    }

    /**
     * 获取用户留言
     * @param $id
     * @param int $pageSum
     * @return \think\Paginator
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUserList($id,$pageSum = 10){
        if ($pageSum <= 0) {
            $pageSum = getSystem("pageSUM") ? getSystem("pageSUM") : 10;
        }

        $list=self::with("user")
            ->where('status',self::SHOW_STATUS)
            ->where('user_car_id',$id)
            ->where('publish_time', '<=',time())
            ->order('create_time desc')
            ->paginate($pageSum);

        foreach ($list as $vo){
            $arr=$this->checkChild($vo);
            $vo->first=$arr[count($arr)-1];

            if(count($arr)>1){
                unset($arr[count($arr)-1]);
                $vo->child=$arr;
            }else{
                $vo->child=[];
            }
        }

        return $list;
    }

    /**
     * 循环所有回复
     * @param $item
     * @return array
     * @throws \think\exception\DbException
     * @throws db\exception\DataNotFoundException
     * @throws db\exception\ModelNotFoundException
     */
    private function checkChild($item){
        $list=self::with("user")
            ->where("id",$item->pid)
            ->where('status',self::SHOW_STATUS)
            ->select();

        foreach ($list as $vo){
            $data=$this->checkChild($vo);
        }
        $data[]=$item->toArray();

        return $data;
    }


    public function getUserReply($id,$pageSum=0,$row=2){
        if($pageSum<=0){
            $pageSum=getSystem("pageSUM")?getSystem("pageSUM"):5;
        }
        //先得到最新的车辆回复
        $list = [];
        $userCar_list = db::view("userCar","id,car_id,mileage,price,card_time")
            ->view("car","car_name,series_thumb","userCar.car_id=car.car_id")
            ->where(["userCar.status"=>1,"userCar.user_id"=>$id])
            ->order("lastreply_time desc")
            ->paginate($pageSum);
        $list = [];
        foreach($userCar_list as $k=>$v){
            $c_id = $v["id"];
            //获取该发布的最新两条留言
            $message_list = db::view("UserCarMessage","user_id,user_car_id,content")
                ->view("member","uid,nickname","UserCarMessage.user_id = member.uid ")
                ->where('UserCarMessage.status',self::SHOW_STATUS)
                ->where("UserCarMessage.user_car_id",$c_id)
                ->order("UserCarMessage.create_time desc")
                ->limit(2)
                ->select();
            if(count($message_list)>0){
                $v["message"] = $message_list;
                $list[] = $v;
            }
        }
        return $list;
    }

}